<template>
  <div v-loading="loading">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
      class="dataForm"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('候选人名称')" prop="candidateName">
            <el-input
              v-model="form.candidateName"
              :placeholder="$t('候选人名称')"
              show-word-limit
              maxlength="20"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('面试岗位')" prop="interviewPost">
            <el-select v-model="form.interviewPost" placeholder="请选择岗位">
              <el-option
                v-for="item in dict.interviewPostArr"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('工作年限')" prop="workYear">
            <el-input
              v-model="form.workYear"
              :placeholder="$t('工作年限')"
              show-word-limit
              maxlength="10"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('期望薪资')" prop="expectSalary">
            <el-input
              v-model="form.expectSalary"
              :placeholder="$t('期望薪资')"
              show-word-limit
              maxlength="10"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('所在城市')" prop="cityRegistered">
            <el-select v-model="form.cityRegistered" placeholder="请选择">
              <el-option
                v-for="item in dict.cityArr"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('现住址')" prop="address">
            <el-input
              v-model="form.address"
              :placeholder="$t('现住址')"
              show-word-limit
              maxlength="100"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('面试人')" prop="interviewPerson">
            <UserSelect
              v-model="form.interviewPerson"
              :placeholder="$t('请选择面试人')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('面试时间')" prop="interviewTime">
            <el-date-picker
              v-model="form.interviewTime"
              clearable
              style="width: 200px"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm"
              format="yyyy-MM-dd HH:mm"
              :placeholder="$t('面试时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('其他情况备注')">
            <el-input
              v-model="form.otherRemark"
              type="textarea"
              :placeholder="$t('其他情况备注')"
              show-word-limit
              maxlength="200"
            />
          </el-form-item>
        </el-col>
        <div v-if="form.actStatus == 'W'">
          <el-col :span="12">
            <el-form-item :label="$t('面试结果')">
              <el-select v-model="form.interviewResult" placeholder="请选择">
                <el-option
                  v-for="item in dict.interviewResultArr"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('面试结果备注')">
              <el-input
                v-model="form.interviewRemark"
                :placeholder="$t('面试结果备注')"
                show-word-limit
                maxlength="100"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('入职情况')">
              <el-select v-model="form.entryStatus" placeholder="请选择">
                <el-option
                  v-for="item in dict.entryCaseArr"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('入职情况备注')">
              <el-input
                v-model="form.entryStatusRemark"
                :placeholder="$t('入职情况备注')"
                show-word-limit
                maxlength="100"
              />
            </el-form-item>
          </el-col>

        </div>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="oa:interview" :btn-show="!otherBtnHide" :menu-key="menuKey" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right">
      <!--保存 取消按钮-->
      <!-- <save-btn has-permi="system:interview" /> -->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="oa:interview" :menu-key="menuKey" />
      <el-button
        v-if="form.actStatus == 'W' && flag"
        type="primary"
        @click="submitForm"
      >保存</el-button>
    </div>
  </div>
</template>

<script>
import { saveHrInterview, getHrInterview } from '@/api/oa/hr/hrInterview'

export default {
  components: {},
  data() {
    return {
      // 菜单标识
      menuKey: 'hr_interview',
      // 表单参数
      form: {},
      flag: false,
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        candidateName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        interviewPost: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        workYear: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        expectSalary: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        address: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        interviewPerson: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        cityRegistered: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        interviewTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      dict: {
        interviewPostArr: [], // 面试岗位
        interviewResultArr: [], // 面试结果
        entryCaseArr: [], // 入职情况
        cityArr: []
      }
    }
  },
  created() {
    this.$bus.$emit('async_done')
    this.initDict()
  },
  methods: {
    // 初始化数据
    init(data, flag) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getHrInterview(data.id).then((response) => {
          this.flag = flag
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        candidateName: undefined,

        interviewPost: undefined,

        workYear: undefined,

        expectSalary: undefined,

        provinceRegistered: undefined,

        cityRegistered: undefined,

        address: undefined,

        interviewPerson: undefined,

        interviewResult: undefined,

        remark: undefined,

        entryStatus: undefined,

        entryStatusRemark: undefined,

        otherRemark: undefined,

        delFlag: undefined,

        actId: undefined,

        actStatus: undefined,
        interviewTime: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveHrInterview(this.form).then((response) => {
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                  this.form.id = response.data.id
                  // 启动流程
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm()
                }
                resolve()
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            })
          } else {
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: function(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      //  processData.title = this.form.actTitle;
      processData.title = '面试候选人-' + this.form.candidateName + '-' + '(' + this.form.interviewTime + ')'
      // 业务表名
      processData.dataTable = 'hr_interview'
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/oa/hr/interview/hrInterviewForm.vue'
      // processData.viewPath = "@/views/anxin/bonus/anxinBonusApplicationView";
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.menuKey
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    },
    initDict() {
      // 初始化字典数据
      this.getDicts('interview_gw').then((response) => {
        this.dict.interviewPostArr = response.data
      })
      this.getDicts('interview_result').then((response) => {
        this.dict.interviewResultArr = response.data
      })
      this.getDicts('entry_case').then((response) => {
        this.dict.entryCaseArr = response.data
      })
      this.getDicts('oa_city').then((response) => {
        this.dict.cityArr = response.data
      })
    }
    // /** 提交按钮 */
    // submitForm: function () {
    //   this.$refs["form"].validate((valid) => {
    //     if (valid) {
    //       this.loading = true;
    //       saveHrInterview(this.form).then((response) => {
    //         if (response.code === 200) {
    //           this.msgSuccess(this.$t("保存成功"));
    //           this.closeForm();
    //         } else {
    //           this.msgError(response.msg);
    //         }
    //         this.loading = false;
    //       });
    //     } else {
    //       this.locationError(document);
    //     }
    //   });
    // },
  }
}
</script>
